package com.mbl.base.config.security;

import com.mbl.base.modular.base.entity.Role;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Data
public class SecurityUser implements UserDetails {
    private Long id;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 头像
     */
    private String avatar;
    /**
     * 昵称
     */
    private String nickname;
    /**
     * 是否启用
     */
    private boolean enabled = true;
    /**
     * 积分
     */
    private Integer integral;

    /**
     * 用户类型
     * 1：管理员 2：用户
     */
    private Integer type = 1;

    /**
     * 角色列表
     */
    private List<Role> roles = new ArrayList<>();
    /**
     * 权限列表
     */
    private List<String> perms = new ArrayList<>();

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        for (Role role : roles) {
            authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName()));
        }
        return authorities;
    }
    @Override
    public String getPassword() {
        return password;
    }
    @Override
    public String getUsername() {
        return username;
    }
    @Override
    public boolean isAccountNonExpired() { // 账号是否过期
        return true;
    }
    @Override
    public boolean isAccountNonLocked() { // 账号是否冻结
        return true;
    }
    @Override
    public boolean isCredentialsNonExpired() { // 账户密码是否过期
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }
}
